/*
 * 
 */
package fr.univtln.superplanning.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import fr.univtln.superplanning.api.ISuperplanningManager;
import fr.univtln.superplanning.api.actor.PersonType;
import fr.univtln.superplanning.gui.MainWindow;

/**
 * This class manage the gestion of the person type.
 * 
 * @author vincent
 * 
 */
public class PersonTypeController implements ActionListener, ListSelectionListener {

	private ISuperplanningManager superplanningManager;

	private MainWindow mainWindow; // view

	private PersonType personType; // model

	public PersonTypeController(ISuperplanningManager superplanningManager, MainWindow mainWindow,
			PersonType personType) {
		this.mainWindow = mainWindow;
		this.superplanningManager = superplanningManager;
		this.personType = personType;
		initializeViewListeners();
	}

	public MainWindow getMainWindow() {
		return mainWindow;
	}

	public void setMainWindow(MainWindow mainWindow) {
		this.mainWindow = mainWindow;
	}

	public ISuperplanningManager getSuperplanningManager() {
		return superplanningManager;
	}

	public void setSuperplanningManager(ISuperplanningManager superplanningManager) {
		this.superplanningManager = superplanningManager;
	}

	public PersonType getPersonType() {
		return personType;
	}

	public void setPersonType(PersonType personType) {
		this.personType = personType;

		updateCurrentPersonTypeView();

	}

	/**
	 * This method will update the view with the current person type.
	 */
	public void updateCurrentPersonTypeView() {

		String selectedName = (String) mainWindow.getTypepersonnelPanelListTypepersonnelPanelList()
				.getSelectedValue();

		// 1. Get the personType by it's name
		personType = superplanningManager.getPersonManager().getPersonTypeByCode(selectedName);

		if (personType != null) {
			// Update the view

			mainWindow.getTypepersonnelPanelListModifyPanelCodeTextfield().setText(
					personType.getCode());
			mainWindow.getTypepersonnelPanelListModifyPanelDescriptionTextfield().setText(
					personType.getDescription());
			mainWindow.getTypepersonnelPanelListModifyPanelNameTextfield().setText(
					personType.getType());
		}

	}

	/**
	 * This methid add the new personType in the persistent Entity (BDD here).
	 */
	public void save() {

		// Create a person type with the field values
		PersonType personType = new PersonType(mainWindow
				.getTypepersonnelPanelListModifyPanelNameTextfield().getText(), mainWindow
				.getTypepersonnelPanelListModifyPanelCodeTextfield().getText(), mainWindow
				.getTypepersonnelPanelListModifyPanelDescriptionTextfield().getText());

		// Test if it's a new person or a insertion

		superplanningManager.getPersonManager().insertPersonType(personType);

	}

	/**
	 * This method initialize the UI with the default filter values
	 */
	public void updateUi() {
		// get all person types
		List<String> typeCodes = superplanningManager.getPersonManager().getAllPersonTypeCodes();

		mainWindow.getTypepersonnelPanelListTypepersonnelPanelList().setListData(
				typeCodes.toArray());

		updateCurrentPersonTypeView();
	}

	/**
	 * This method will initialize the listener of the view components.
	 */
	public void initializeViewListeners() {
		// Retreive the components and add it's listener.

		// 1. For the selection / visualization
		mainWindow.getTypepersonnelPanelListTypepersonnelPanelList().addListSelectionListener(this);

		// 2. For the insertion / modification
		mainWindow.getTypepersonnelPanelListModifyPanelAddButton().addActionListener(this);
		mainWindow.getTypepersonnelPanelListModifyPanelCancelButton().addActionListener(this);
		mainWindow.getTypepersonnelPanelListModifyPanelModifyButton().addActionListener(this);

		// TODO: where is the new button ?
		mainWindow.getSectionPanelModifyPanelNewButton1().addActionListener(this);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == mainWindow.getTypepersonnelPanelListModifyPanelAddButton()) {
			// Juste save the current person type ;)
			save();

			// and update the IU
			updateUi();

		} else if (e.getSource() == mainWindow.getTypepersonnelPanelListModifyPanelCancelButton()) {

		} else if (e.getSource() == mainWindow.getTypepersonnelPanelListModifyPanelModifyButton()) {

		} else if (e.getSource() == mainWindow.getSectionPanelModifyPanelNewButton1()) {

		}

	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		if (e.getSource() == mainWindow.getTypepersonnelPanelListTypepersonnelPanelList()) {
			updateCurrentPersonTypeView();
			// TODO: gérer le fait qu'un clic = 2 appels
		}
	}
}
